package com.daguchuangyi.erp_android.production_detail.widget;

import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.daguchuangyi.erp_android.ErpApplication;
import com.daguchuangyi.erp_android.R;
import com.daguchuangyi.erp_android.model.product.Product;
import com.daguchuangyi.erp_android.production_detail.BigPictureActivity;

import java.util.ArrayList;
import java.util.List;

import me.relex.circleindicator.CircleIndicator;

/**
 * Created by lijianjian on 2018/3/11.
 */

public class ProductCoverView extends ConstraintLayout {
    private ViewPager mViewPager;
    private CoverAdapter mCoverAdapter;
    private CircleIndicator mIndicator;

    private List<String> mCoverUrls = new ArrayList<>();
    private List<String> mName = new ArrayList<>();

    public void bindProductInfo(Product product) {
        mName.clear();
        mCoverUrls.clear();
        mCoverUrls.add(product.getExhibitionImage());
        mName.add(product.getPNO() + "_COVER.png");
        mCoverUrls.add(product.getCassetteImage());
        mName.add(product.getPNO() + "_CAD.png");
        mCoverAdapter.setImgUrls(mCoverUrls);
        mIndicator.setViewPager(mViewPager);
    }

    public ProductCoverView(Context context) {
        super(context);
        init(context);
    }

    public ProductCoverView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ProductCoverView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        inflate(context, R.layout.view_product_cover_layout, this);
        mIndicator = findViewById(R.id.indicator);
        mViewPager = findViewById(R.id.cover_vp);
        mCoverAdapter = new CoverAdapter(getContext());
        mViewPager.setAdapter(mCoverAdapter);
    }

    private class CoverAdapter extends PagerAdapter {
        private List<String> mImgUrls = new ArrayList<>();
        Context mContext;
        LayoutInflater mLayoutInflater;

        public void setImgUrls(List<String> imgUrls) {
            mImgUrls = imgUrls;
            notifyDataSetChanged();
        }

        public CoverAdapter(Context context) {
            mContext = context;
            mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return mImgUrls.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View itemView = mLayoutInflater.inflate(R.layout.item_cover_layout, container, false);

            String title = mName.get(position);
            String url = mImgUrls.get(position);
            ImageView imageView = itemView.findViewById(R.id.item_cover_iv);

            RequestOptions requestOptions = new RequestOptions()
                    .placeholder(R.color.normalGrey)
                    .error(R.color.normalGrey)
                    .diskCacheStrategy(DiskCacheStrategy.ALL);

            Glide.with(ErpApplication.getAppContext())
                    .load(url)
                    .apply(requestOptions)
                    .into(imageView);

            container.addView(itemView);
            itemView.setOnClickListener(view -> BigPictureActivity.start(getContext(), url, title));

            return itemView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }


}
